package com.shujia.hive;

import java.sql.*;

public class HiveJDBC {
    public static void main(String[] args) throws SQLException {
        // 建立连接
        Connection conn = DriverManager.getConnection("jdbc:hive2://master:10000/test01", "root", "");

        // 创建Statement：要不要传参
        // 传参：PrepareStatement，不需要传参直接构建Statement
        // 避免SQL注入
        Statement st = conn.createStatement();

        // 执行SQL
        ResultSet rs = st.executeQuery("select * from students");

        // 遍历结果
        while (rs.next()) {
            long id = rs.getLong("id");
            String name = rs.getString("name");
            int age = rs.getInt("age");
            String gender = rs.getString("gender");
            String clazz = rs.getString("clazz");

            System.out.println(id + "," + name + "," + age + "," + gender + "," + clazz);
        }

        // 关闭连接
        rs.close();
        st.close();
        conn.close();

    }
}
